/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : club_write.js                                 
*  Description  : 개별클럽 일반게시판 글작성 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-14  이윤노    Created.  
*  2012-05-03  김효순    Complete.  
*******************************************************************************/

jQuery(function($){
	//상단메뉴
	function clsTopMenuInfo(){
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsTopMenuInfo.prototype = {
		_init: function(){
			var $layer = $("div#container nav ul.self_club_gnb");			
			this._HTMLDom = {
				"$layer":$layer
			}			
		}
		, getBtnClubHome: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:first");
		}
		, getBtnClubField: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(1)");
		}
		, getBtnClubBoard: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(2)");
		}
		, getBtnClubPic: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(3)");
		}
		, getBtnClubAdmin: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(4)");
		}
	}
	
	//공지사항
	function clsNotice(){
		this._HTMLDom = null;
		this._init();
	}
	
	clsNotice.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content p.tb_ckeck input:checkbox");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			return (this._HTMLDom["$layer"].prop("checked")==true?"Y":"N");
		}
		, setValue: function(val){
			this._HTMLDom["$layer"].prop("checked",val);
		}
			
			
	}
	
	// 제목
	function clsTitle()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "제목을 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsTitle.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content dl.golf_write :text");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);
		}
		, getError: function(){
			return this.errMsg;
		}
		, isValid: function(){
			if(this._HTMLDom["$layer"].val() == ""){
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;
			}
			
			return true;
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
	}
	
	// 내용
	function clsNote()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsNote.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content dl.golf_write dd:first");
			var thisClass = this;
			
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.html("<textarea></textarea>");
			
			fnLoadingJS("../inc/ckeditor_3.6.3/ckeditor.js", function(){
				fnLoadingJS("../inc/ckeditor_3.6.3/adapters/jquery.js", function(){
					thisClass._CKEditor();
				});
			});
		}
		, _CKEditor: function(){
			var thisClass = this;
			
			this._HTMLDom["$layer"].find("textarea").ckeditor(document.objGlobal.getCKOption());
				
			// 에디터 준비완료
			this.$getEditor().ckeditorGet().on("instanceReady", function(){
				
			});
		}
		, focus: function(){
			this.$getEditor().ckeditorGet().focus();
		}
		, $getEditor: function(){
			return this._HTMLDom["$layer"].find("textarea");
		}
		, getValue: function(){
			return this.$getEditor().val();
		}
		, setValue: function(val){
			this.$getEditor().val(val);
		}
	}
	
	
	function clsComm()
	{
		this.clubRequest = null;
		this.clubData = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.clubData = {
				"제목": null
				, "내용": null
			}
			
			this.clubRequest = {
				"클럽키": null
				,"게시판키" : null				
			}
		}
		, setClubRequest: function(key, val){
			this.clubRequest[key] = val;
		}
		, getClubRequest: function(key){
			return this.clubRequest[key];
		}
		//게시글 조회(수정시만 조회)
		, getClubInfo: function(){
			var thisClass = this;
			var szBoard_seq = this.clubRequest["게시판키"];
			
			//새글인경우 리턴
			if(!szBoard_seq) return;
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CB001",
				"servicename":"getDetailClubBoard"		
				,"listcount":10
				,"currpageno":1		
			}, {
				"board_seq": szBoard_seq			
				, "modpage_yn": (szBoard_seq?"Y":"N")
			},  function(ret){
				
				var msg = document.objGlobal.getResultMsg("getDetailClubBoard", ret["msp"]["contents"]["serviceResult"]);
								
				if(msg) alert(msg); 
				
				// callback and mapping
				if(ret["msp"]["contents"]["items"]){
					thisClass.clubData = {					
						"제목": ret["msp"]["contents"]["items"]["title"]
						, "댓글수": ""
						, "작성자": {
							"아이디": ret["msp"]["contents"]["items"]["reg__id"]
							, "이미지": "../images/board/default_50_50.gif"
							, "닉네임": ret["msp"]["contents"]["items"]["reg_nickname"]
						}
						, "작성일": ret["msp"]["contents"]["items"]["reg__date"]
						, "작성시":  ret["msp"]["contents"]["items"]["reg__time"]
						, "조회수": ret["msp"]["contents"]["items"]["hit"]
						, "글권한": ""
						, "내용": ret["msp"]["contents"]["items"]["content"]					
					}
				}
				
				$(thisClass).trigger("evtFinishClubInfo");
			},true);
			// ajax 끝				
		
		}
		, getClubData: function(){
			return this.clubData;
		}
		//게시글 등록
		, clubBoardInsert: function(arg){
			var thisClass = this;
			var szClub_seq = this.clubRequest["클럽키"];
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CB001",
				"servicename": "regClubBoard"
			}, {
				"club_seq": szClub_seq
				,"board_type": "D"
				,"title": arg["제목"]
				,"notice_yn": arg["공지"]
				,"content": (arg["내용"]).replace(/\n/g,"")					
				
			}, function(ret){				
				var msg = document.objGlobal.getResultMsg("clubBoardInsert", ret["msp"]["contents"]["serviceResult"]);
				
				if(msg) alert(msg);
				
				// callback and mapping
				$(thisClass).trigger("evtFinishClubSubmit", [ret]);
				
			},true);
			// ajax 끝
			
		}
		//게시글 수정
		, clubBoardUpdate: function(arg){
			var thisClass = this;
			var szBoard_seq = this.clubRequest["게시판키"];
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CB001",
				"servicename": "updateClubBoard"
			}, {
				"board_seq": szBoard_seq
				,"board_type": "D"
				,"title": arg["제목"]
				,"notice_yn": arg["공지"]
				,"content": (arg["내용"]).replace(/\n/g,"")					
				
			}, function(ret){				
				var msg = document.objGlobal.getResultMsg("clubBoardUpdate", ret["msp"]["contents"]["serviceResult"]);
				
				if(msg) alert(msg);
				
				// callback and mapping
				$(thisClass).trigger("evtFinishClubSubmit", [ret]);
				
			},true);
			// ajax 끝
			
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objTitle = null;
		this.objNote = null;
		this.objNotice = null;
		this.objTopMenuInfo = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objTitle = new clsTitle();
			this.objNote = new clsNote();
			this.objNotice = new clsNotice();
			this.objTopMenuInfo = new clsTopMenuInfo();
						
			this._bind();
			
			this.objComm.setClubRequest("클럽키", document.objUtil.getParam("club_seq"));
			this.objComm.setClubRequest("게시판키", document.objUtil.getParam("board_seq"));
				
			// 글정보 받아오기
			if(document.objUtil.getParam("board_seq")){
				document.objGlobal.spinStart("receiving...");
				
				this.objComm.getClubInfo();
			}
		}
		, _bind: function(){
			var thisClass = this;

			//<--navi 페이지 설정 시작
			//홈
			this.objTopMenuInfo.getBtnClubHome().click(function(event){
				event.preventDefault();						
				document.location.href = "home.html?club_seq=" + (document.objUtil.getParam("club_seq"));
			});
			//동호회필드
			this.objTopMenuInfo.getBtnClubField().click(function(event){
				event.preventDefault();						
				document.location.href = "club_field.html?club_seq=" + (document.objUtil.getParam("club_seq"));
			});
			//일반게시판
			this.objTopMenuInfo.getBtnClubBoard().click(function(event){
				event.preventDefault();						
				document.location.href = "club_list.html?club_seq=" + (document.objUtil.getParam("club_seq"));
			});
			//사진게시판
			this.objTopMenuInfo.getBtnClubPic().click(function(event){
				event.preventDefault();						
				document.location.href = "club_picture_list.html?club_seq=" + (document.objUtil.getParam("club_seq"));
			});
			//관리하기
			this.objTopMenuInfo.getBtnClubAdmin().click(function(event){
				event.preventDefault();						
				document.location.href = "club_admin_info.html?club_seq=" + (document.objUtil.getParam("club_seq"));지				
			});
			//navi 페이지 설정 시작 -->
			
			// 수정모드시 데이터 받아오기 완료
			$(this.objComm).bind("evtFinishClubInfo", function(){
				var thisData = this.getClubData();
				
				document.objGlobal.spinEnd();
				
				thisClass.objTitle.setValue(thisData["제목"]);
				thisClass.objNote.setValue(thisData["내용"]);
				thisClass.objNotice.setValue((thisData["공지"]=="0"?false:true));
				//$("div#container section#main_content p.tb_ckeck input:checkbox").prop("checked",(thisData["공지"]=="0"?false:true));
				
			// 글작성/수정 완료
			}).bind("evtFinishClubSubmit", function(){
				var ret = arguments[1];
				
				document.objGlobal.spinEnd();
				
				document.location.href = "club_list.html?club_seq=" + document.objUtil.getParam("club_seq");
			});
			
			// 글작성
			$("div#container section#main_content a").has("img[alt=확인]").click(function(event){
				event.preventDefault();				
				
				if(!thisClass.objTitle.isValid()){
					alert(thisClass.objTitle.getError());
					thisClass.objTitle.focus();
					return;
				}
				
				document.objGlobal.spinStart("sending...");
				
				if(thisClass.objComm.getClubRequest("게시판키")){
					thisClass.objComm.clubBoardUpdate({
																				 "제목": thisClass.objTitle.getValue()
																				, "내용": thisClass.objNote.getValue()
																				, "공지": thisClass.objNotice.getValue()
																			});	
				}else{
					thisClass.objComm.clubBoardInsert({
																				 "제목": thisClass.objTitle.getValue()
																				, "내용": thisClass.objNote.getValue()
																				, "공지": thisClass.objNotice.getValue()
																			});	
				}
				
			});
		}
	}
	
	document.objControl = new clsControl();
});